package com.gmzg.gmzgnews.news.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.news.WebViewActivity;
import com.gmzg.gmzgnews.news.adapters.NewsAdapter;
import com.gmzg.gmzgnews.news.entries.AmuseNews;
import com.gmzg.gmzgnews.news.entries.AmuseTop;
import com.gmzg.gmzgnews.news.enums.NewsType;
import com.gmzg.gmzgnews.news.interfaces.NeteaseServer;
import com.jash.networklibrary.NetworkTask;
import com.jash.networklibrary.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class AmuseFragment extends BaseFragment implements NetworkTask.Callback<AmuseTop>,NewsAdapter.OnChildClickListener {

    private static final String TAG = "AmuseFragment";
    @BindView(R.id.news_amuse_recycleView)
    RecyclerView mNewsAmuseRecycleView;
    private List<AmuseNews> mHeadNewsList;
    private NeteaseServer mNewsAmuseServer;
    private NewsAdapter mNewsAmuseAdapter;

    public AmuseFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return "娱乐";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_news_amuse, container, false);
        ButterKnife.bind(this, ret);
        mHeadNewsList = new ArrayList<>();
        mNewsAmuseAdapter = new NewsAdapter(getContext(),mHeadNewsList, NewsType.TYPE_HEAD);
        mNewsAmuseRecycleView.setLayoutManager(
                         new LinearLayoutManager(getContext(),
                        LinearLayoutManager.VERTICAL,false));
        mNewsAmuseRecycleView.setAdapter(mNewsAmuseAdapter);
        mNewsAmuseServer = Tools.getInstance(NeteaseServer.class);
        mNewsAmuseServer.getAmuseData(1,50).execute(this);
        mNewsAmuseAdapter.setOnChildClickListener(this);
        return ret;
    }


    @Override
    public void onSuccess(AmuseTop text) {
        mNewsAmuseAdapter.addAll(text.getTngou());
    }

    @Override
    public void onFail(Exception e) {

    }

    @Override
    public void onChildClick(RecyclerView parent, View view, int position, AmuseNews data) {
        Toast.makeText(getContext(), data.getFromurl()+"被点击了", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getContext(), WebViewActivity.class);
        intent.putExtra("997", mHeadNewsList.get(position).getFromurl());
        startActivity(intent);
    }
}
